package com.swust.excel.read.morsheet;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.List;

/**
 * @author: Chen Yixing
 * @since: 2023/11/16 16:04:47
 * @description:
 */
@Slf4j
public class ReadMorMain {

    public static void main(String[] args) {
        try {
            /*
            * 如果是网页接口，读取模板得换一种形式，如下所示
                    RiverAffairsOrder dto = riverAffairsOrders.get(0);
                    //返回体设置
                    response.setContentType("application/vnd.ms-excel");
                    response.setCharacterEncoding("utf-8");
                    String excelName = dto.getCreateTimeYm() + "_" + dto.getUnitName() + ".xlsx";
                    excelName = URLEncoder.encode(excelName, "UTF-8");
                    response.setHeader("Content-disposition", "attachment;filename=" + excelName);
                    //
                    ClassPathResource classPathResource = new ClassPathResource(File.separator + "templates" + File.separator + "river_affairs.xlsx");
                    EasyExcel.write(response.getOutputStream(), RiverAffairsOrder.class)
                            .withTemplate(classPathResource.getInputStream())
                            .autoCloseStream(Boolean.FALSE)
                            .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                            .sheet()
                            .doFill(dto);
            * */
            String fileName = ReadMorMain.class.getResource("/").getPath() + "read/morsheet.xlsx";
            File file = new File(fileName);
            ExcelReader reader = EasyExcel.read(file).build();
            List<ReadSheet> sheets = reader.excelExecutor().sheetList();
            for (int i = 0; i < sheets.size(); i++) {
                ReadSheet readSheet = sheets.get(i);
                System.out.println("表序号：" + readSheet.getSheetNo());
                System.out.println("表名：" + readSheet.getSheetName());
                List<Object> objects = EasyExcel.read(file, ReadMorBean.class, new ReadMoreListener()).sheet(i).doReadSync();
            }
        } catch (Exception e) {
            log.info(e.getMessage(), e);
        }
    }
}
